$(function () {
    var obj = {
        a:{
            1:{
                11:{
                    12:0,72:0},
                12:{
                    15:0,52:0},
            },

            2:{
                12:{
                    19:0}},
            3:{
                13:{
                    16:0}}},
        b:{
            4:{
                14:{
                    124:0}},
            5:{
                15:{
                    42:0}},
            6:{
                16:{
                    62:0}}},
        c:{
            7:{
                17:{
                    92:0}},
            8:{
                18:{
                    22:0}},
            9:{
                19:{
                    12:0}}},
    };
    //遍历对象，获得列表
    var app = function (listid,data) {
        //$(listid).empty();
        for(var x in data){
            if(listid == "#list2"){
                var txt1 ="<button class='list-group-item'>" + x + "<span title='remove State View' class='glyphicon glyphicon-trash remCity' style='float: right;'></span></button>";
                $("#list2").append(txt1);
                $("#list2").show();
                $("#list3").hide();
                $("#street").hide();
                $("#city").show();
                $("#addStreet").hide();
            }else if(listid == "#list3"){
                var txt3 = "<button class='list-group-item'>" + x + "<span title='remove City View' class='glyphicon glyphicon-trash remParent' style='float: right;'></span></button>";
                $("#list3").append(txt3);
                $("#street").show();
                $("#list3").show();
            }
        }
    }
    //添加对象
    var addObj = function (bntid,textid,listid,tex,list1id) {
        $(bntid).click(function () {
            var streetVal = $(textid).val();
            if(streetVal != ""){
                if(listid == "#list2"){
                    var txt = "<button class='list-group-item'>" + streetVal + "<span title='remove State View' class='glyphicon glyphicon-trash remCity' style='float: right;'></span></button>";
                    $(listid).append(txt);
                    $(list1id).find("button").remove();
                }else if(listid == "#list3"){
                    var txt = "<button class='list-group-item'>" + streetVal + "<span title='remove City View' class='glyphicon glyphicon-trash remParent' style='float: right;'></span></button>";
                    $(listid).append(txt);
                }

            }
            document.getElementById(tex).value = "";
        })
    }
    //删除对象
    var remObj = function (remClass,listid) {
        $(remClass).click(function () {
            $(this).parent().remove();
            $(listid).hide();
        })
    }
    //添加城市按钮点击显示，隐藏
    var togg = function (id,add) {
        $(id).click(function () {
            $(add).toggle();
        })
    }

    var h = $("#aaaa").html();
    var nums = h.split(',');
    for(var i in nums){
        var z = nums[i].substring(nums[i].indexOf(':')+4,nums[i].indexOf('}')-1);
        var txt = "<button class='list-group-item World'>" + z + "</button>";
        $("#country").append(txt);
    }
    $("#city,#street,#list2,#list3,#addCity,#addStreet").hide();
    //$("#street").hide();
    //$("#list2").hide();
    //$("#list3").hide();
    //$("#addCity").hide();
    //$("#addStreet").hide();
    $("#country >button").click(function () {
        index = this.innerHTML;
        test();
        var h = $("#bbbb").html();
        console.log(h);

        $("#list1 button").click(function () {
            var index1 = this.innerHTML;
            var data1 = data[index1];
            app("#list2",data1);
            $("#addCity").hide();
        $("#list2 button").click(function () {
            var index2 = $(this).text();
            var data2 = data1[index2];
            app("#list3",data2);
            remObj(".remCity","#list3");
            remObj(".remParent");
            $("#addStreet").hide();
            $("#list3 button").click(function () {
                $("#addCity").hide();
            })
        })
        })

    })
    togg("#city","#addCity");
    togg("#street","#addStreet");
    addObj("#btnCity","#cityText","#list2","cityText","#list3");
    addObj("#btnStreet","#streetText","#list3","streetText","#list3");

    function test() {
	console.log(index);
        $.ajax({
            url:'/mapcms/'+index,
            contentType:'application/json',
            data:JSON.stringify({name:index},null,'\t'),
            type:'POST',
            success:function (data) {
                console.log(data["var1"]);
            },
            error: function() {
                console.log("bbbb");
            }
        })
    }
})
